﻿using System;

namespace Program19
{
    class Program
    {
        static void Main(string[] args)
        {
            //1.用方法来实现：有一个字符串数组：{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" },请输出最长的字符串。
            string[] names = {"马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特"};
            Console.WriteLine("最长的字符串是:"+GetLongestString(names)+"\n"); 

            //2.用方法来实现：请计算出一个整型数组的平均值。{ 1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10 }。要求：计算结果如果有小数，则显示小数点后两位（四舍五入）。
            int[] nums = {1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10};
            Console.WriteLine("平均值:"+GetAvg(nums)+"\n");

            //3.请通过冒泡排序法对整数数组{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }实现升序排序。
            int[] numbers = {1, 3, 5, 7, 90, 2, 4, 6, 8, 10};
            BubbleSort(numbers);
            

        }

        /// <summary>
        /// 取最长的字符串
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public static string GetLongestString(string[] names)
        {
            string max = names[0];
            for (int i = 0; i < names.Length; i++)
            {
                if (names[i].Length > max.Length)
                {
                    max = names[i];
                }

            }

            return max;
        }


        
        //public static string GetMax(string[] name)
        //{
        //    string max = name[0];
        //    for (int i = 0; i < name.Length; i++)
        //    {
        //        if (max.Length<name[i].Length)
        //        {
        //            max = name[i];
        //        }
        //    }
        //    return max;
        //}

        /// <summary>
        /// 求整型数组的平均值
        /// </summary>
        /// <param name="nums"></param>
        /// <returns></returns>
        public static double GetAvg(int[] nums)
        {
            int sum = 0;
            for (int i = 0; i < nums.Length; i++)
            {
                sum += nums[i];
            }

            double round= (double)sum / nums.Length;
            return Math.Round(round, 2);
        }

        /// <summary>
        /// 冒泡排序
        /// </summary>
        /// <param name="nums"></param>
        public static void BubbleSort(int[] nums)
        {
            Console.WriteLine("排序前：");
            for (int i = 0; i < nums.Length; i++)
                Console.Write(nums[i]+" ");
            Console.WriteLine();
            Console.WriteLine("排序后：");
            for (int i = 0; i < nums.Length; i++)
            {
                for (int j = 0; j < nums.Length-1; j++)
                {
                    if (nums[j] > nums[j + 1])
                    {
                        var temp = nums[j];
                        nums[j] = nums[j + 1];
                        nums[j + 1] = temp;
                    }
                }
            }
            for (int i = 0; i < nums.Length; i++)
                Console.Write(nums[i] + " ");
            Console.WriteLine();
        }

        //public static void Bubbling(int[] n)
        //{
        //    var r = 0;
        //    for (int i = 0; i < n.Length; i++)
        //    {
        //        for (int j = 0; j < n.Length-1; j++)
        //        {
        //            if (n[j] > n[j + 1])
        //            {
        //                r = n[j];
        //                n[j] = n[j + 1];
        //                n[j + 1] = r;
                        
        //            }
        //        }
        //    }
        //    foreach (var item in n)
        //    {
        //        Console.Write(item+" ");
        //    }
        //}
    }
}
